package com.df.springmvc.common.util;

import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.methods.PostMethod;

public class HTTPClientUtil{
	public static void main(String[] args){
		Object post=HTTPClientUtil.post("http://www.baidu.com",null);
		System.out.println(post);
	}
	public static Object post(String url,String json){
		// TODO Auto-generated method stub
		HttpClient httpClient=new HttpClient();
		PostMethod method=new PostMethod(url);
		method.setRequestHeader("content-type","application/x-www-form-urlencoded; charset=utf-8");
		// method.setRequestHeader("accept","application/json");
		try{
			method.addParameter("json",json);
			httpClient.executeMethod(method);
			StatusLine statusLine=method.getStatusLine();
			System.out.println(statusLine);
			String responseBodyAsString=method.getResponseBodyAsString();
			return responseBodyAsString;
		}catch(HttpException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
